
#region Imported Namespaces
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	#region Class Annotation
	/// <summary>
	/// The Annotation allows creation of new annotation information which includes the following:
	/// date, description, title, and user name.
	/// </summary>
	[ToolboxData("<{0}:Annotation runat=server />")]
	[Designer(typeof(AnnotationDesigner), typeof(IDesigner))]
	public class Annotation : System.Web.UI.WebControls.WebControl, INamingContainer
	{
		#region Instance Variables
		private TextBox	date;
		private TextBox	description;
		private TextBox	title;
		private TextBox	userName;
		#endregion

		#region Properties
		/// <summary>
		/// Get date. 
		/// </summary>
		[Bindable(true), Category("Behavior"), DefaultValue("")] 
		public string Date
		{
			get { EnsureChildControls(); return date.Text;	}
			set	{ EnsureChildControls(); date.Text = value; }
		}

		/// <summary>
		/// Get description. 
		/// </summary>
		[Bindable(true), Category("Behavior"), DefaultValue("")] 
		public string Description
		{
			get { EnsureChildControls(); return description.Text;	}
			set	{ EnsureChildControls(); description.Text = value; }
		}

		/// <summary>
		/// Get title. 
		/// </summary>
		[Bindable(true), Category("Behavior"), DefaultValue("")] 
		public string Title
		{
			get { EnsureChildControls(); return title.Text;	}
			set	{ EnsureChildControls(); title.Text = value; }
		}

		/// <summary>
		/// Get user name. 
		/// </summary>
		[Bindable(true), Category("Behavior"), DefaultValue("")] 
		public string UserName
		{
			get { EnsureChildControls(); return userName.Text;	}
			set	{ EnsureChildControls(); userName.Text = value; }
		}

		/// <summary>
		/// DescriptionRows adjusts the number of rows in the description text box. 
		/// </summary>
		[Bindable(true), Category("Behavior"), DefaultValue("1")] 
		public int DescriptionRows
		{
			get 
			{ 
				EnsureChildControls(); 
				return description.Rows;	
			}
			set	
			{ 
				EnsureChildControls(); 
				if (value > 1)
					description.TextMode = TextBoxMode.MultiLine;

				description.Rows = value; 
			}
		}

		#endregion

		#region Overrides
		/// <summary>
		/// Create all controls that will be on the Annunciator. Notice that the creation is more than just instantiating
		/// the controls. It also involves in generating the underlying HTML so that the controls shows up per the need.
		/// </summary>
		protected override void CreateChildControls()
		{
			CreateControls();
			GenerateControlTree();
		}

		/// <summary>
		/// Renders all contents.
		/// </summary>
		/// <param name="writer">The writer through which contents are rendered</param>
		protected override void Render(HtmlTextWriter writer)
		{
			RenderContents(writer);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Create all controls in the Header.
		/// </summary>
		private void CreateControls()
		{
			Controls.Clear();

			userName = new TextBox();
			userName.Text = UserName;
			
			date = new TextBox();
			date.Text = DateTime.Now.ToString();
			date.Enabled = userName.Enabled = false;

			title = new TextBox();
			title.CssClass = "WideTextBox";

			description = new TextBox();
			description.Wrap = true;
			description.CssClass = "WideTextBox";
		}

		/// <summary>
		/// Setup the control tree to reflect the actual (and exact) layout of the controls within the Header.
		/// </summary>
		private void GenerateControlTree()
		{
			Controls.Add(new LiteralControl(
				"<table>\n" +
				"\t<tr>\n" +
				"\t\t<td align=\"right\">User Name:&nbsp;</td>\n" +
				"\t\t<td>"));
			Controls.Add(userName);
			Controls.Add(new LiteralControl(
				"</td>\n" +
				"\t</tr>\n" +
				"\t<tr>\n" +
				"\t\t<td align=\"right\">Date:&nbsp;</td>\n" +
				"\t\t<td>"));
			Controls.Add(date);
			Controls.Add(new LiteralControl(
				"</td>\n" +
				"\t</tr>\n" +
				"\t<tr>\n" +
				"\t\t<td align=\"right\">Title:&nbsp;</td>\n" +
				"\t\t<td width=\"100%\">"));
			Controls.Add(title);
			Controls.Add(new LiteralControl(
				"</td>\n" +
				"\t</tr>\n" +
				"\t<tr>\n" +
				"\t\t<td align=\"right\" valign=\"top\">Description:&nbsp;</td>\n" +
				"\t\t<td width=\"100%\">"));
			Controls.Add(description);
			Controls.Add(new LiteralControl(
				"</td>\n" +
				"\t</tr>\n" +
				"</table>"));
		}
		#endregion

		#region Internal Methods
		/// <summary>
		/// This method is ONLY invoked by the associated designer to show the control contents at design time.
		/// </summary>
		internal void Design()
		{
			EnsureChildControls();
		}
		#endregion
	}
	#endregion

	#region Class AnnotationDesigner
	/// <summary>
	/// Designer associated with the control.
	/// </summary>
	public class AnnotationDesigner : ControlDesigner
	{
		#region Overrides
		/// <summary>
		/// Force the control to create its child controls for display at design time.
		/// </summary>
		/// <returns>HTML representation of the control</returns>
		public override string GetDesignTimeHtml()
		{
			((Annotation)Component).Design();
			return base.GetDesignTimeHtml();
		}
		#endregion
	}
	#endregion
}

// --- EOF ---
